<?php
/**
 * Created by PhpStorm.
 * User: chenx
 * Date: 18-8-9
 * Time: 下午3:09
 */

namespace shopend\controllers;

use common\models\Complaint;
use common\models\ComplaintImg;
use common\models\CountComplaint;
use common\service\CommonService;
use common\service\ConstantService;
use common\service\GlobalUrlService;

class ComplaintController extends BaseController
{
    //列表
    public function actionIndex(){
        if($this->isAjax()) {
            $query = Complaint::find();
            $query->andWhere(['shop_id' => $this->shop['id']]);

            if (!empty($this->get('search_type')) && !empty($this->get('search_value'))) {
                if ($this->get('search_type') == 1) {
                    //按手机号搜索
                    $query->andWhere(['like', 'user_phone', $this->get('search_value')]);

                } else {
                    //按店铺名称搜索
                    $query->andWhere(['like', 'user_name', $this->get('search_value')]);

                }
            }
            if (!empty($this->get('status'))) {
                $query->andWhere(['status' => $this->get('status') - 1]);
            }

            if (!empty($this->get('start_time'))) {
                $query->andWhere(['>=', 'create_time', $this->get('start_time')]);
            }
            if (!empty($this->get('end_time'))) {
                $query->andWhere(['<=', 'create_time', $this->get('start_time')]);
            }

            $p = intval($this->get('p', 1));
            $p = ($p > 0) ? $p : 1;
            $pages = CommonService::ipagination([
                'total_count' => $query->count(),
                'page_size' => $this->get('limit',10),
                'page' => $p,
                'display' => 10
            ]);

            $offset = ($p - 1) * $this->get('limit',10);

            $lists = $query->orderBy(['id' => SORT_DESC])
                ->offset($offset)
                ->limit($this->get('limit',10))
                ->asArray()
                ->all();
            $count = $query->count();
            return $this->renderPageJson($lists,$count,ConstantService::$response_tab_success);
        }
        return $this->render('index');
    }

    //查看
    public function actionInfo(){
            $id = $this->get('id');
            $row = Complaint::findOne(['id'=>$id]);
            $row = $row->toArray();
            $images = ComplaintImg::find()->where(['complaint_id'=>$id])->asArray()->all();
            if(!empty($images)){
                foreach ($images as &$image){
                    $image['image'] = GlobalUrlService::buildCdnUrl($image['image']);
                }
             }
            $row['image'] = $images;
        return $this->render('info',['row'=>$row]);
    }

    //审核通过或不通过
    public function actionChangeStatus(){
        if($this->isPost()){
            $id = $this->post('id');
            $status = $this->post('status');
            $dismissal = $this->post('dismissal');
            $row = Complaint::findOne(['id'=>$id]);
            //修改为处理中
            $row->admin_id = $this->shop['id'];
            $row->admin_name = $this->shop['shop_name'];
            $row->status = $status;
            $row->dismissal = $dismissal;
            $row->update_time = time();
            if($row->save()){
                $time = strtotime(date('Y-m-d',$row['create_time']));
                $this->_addcount($status,$time);
                return $this->renderPageJson([],'操作成功',ConstantService::$response_success);
            }
            return $this->renderPageJson([],'操作失败',ConstantService::$response_fail);

        }
        return $this->renderPageJson([],'参数错误',ConstantService::$response_fail);
    }

    //完成
    public function actionUpdateComplete(){
        if($this->isAjax() && $this->isPost()){
            $id = $this->post('id');
            $result = $this->post('result');

            if(empty($id) || empty($result)){
                return $this->renderPageJson([],'参数错误',ConstantService::$response_fail);
            }

            $row = Complaint::findOne(['id'=>$id]);
            $row->admin_id = $this->shop['id'];
            $row->admin_name = $this->shop['shop_name'];
            $row->result = $result;
            $row->status = 2;
            $row->complete_time = time();
            if($row->save()){
                $time = strtotime(date('Y-m-d',$row['create_time']));
                $this->_addcount(2,$time);
                return $this->renderPageJson([],'操作成功',ConstantService::$response_success);
            }
            return $this->renderPageJson([],'操作是吧,请稍后再试!',ConstantService::$response_fail);

        }
        return $this->renderPageJson([],'参数错误',ConstantService::$response_fail);
    }

    /**
     * @param $sale_type
     * @return bool
     */
    private function _addcount($status,$time){
        $row = CountComplaint::findOne(['time'=>$time]);
        $pass_num = 0;
        $fail_num = 0;
        $completion_num = 0;
        if($status == 1){
            $pass_num = 1;
        }elseif($status == 3){
            $fail_num = 1;
        }elseif($status == 2){
            $completion_num= 1;
        }
        //修改申请数
        $row->pass_num = $row['pass_num'] + $pass_num;
        $row->fail_num = $row['fail_num'] + $fail_num;
        $row->completion_num = $row['completion_num'] + $completion_num;
        $row->wait_handle_num = $row['wait_handle_num'] - 1;
        $row->update_time = time();
        $re = $row->save();
        if($re){
            return true;
        }
        return false;
    }

}